package com.sharesrc.umangadownloader.server.services.browse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Connection;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.sharesrc.umangadownloader.server.ServerConstant;
import com.sharesrc.umangadownloader.server.core.ConnectionMgr;
import com.sharesrc.umangadownloader.shared.common.Constant.MangaSites;
import com.sharesrc.umangadownloader.shared.model.ChapterInfo;
import com.sharesrc.umangadownloader.shared.model.MangaDesc;
import com.sharesrc.umangadownloader.shared.model.MangaInfo;
import com.sharesrc.umangadownloader.shared.model.MangaSiteInfo;
import com.sharesrc.umangadownloader.shared.model.SitePageInfo;

public enum VnSharingBrowser implements IFBrowser {

	INSTANCE;

	private VnSharingBrowser() {

	}

	@Override
	public MangaSiteInfo getMangaSiteInfo(String childSiteId) {
		MangaSiteInfo result = new MangaSiteInfo(MangaSites.VNSHARING.getValue(), childSiteId);

		try {
			String url = ServerConstant.Protocols.HTTP + ServerConstant.SiteInfo.VNSHARING_BASE_URL + ServerConstant.SiteInfo.VNSHARING_LIST_PATH;
			url += childSiteId.length() == 0 ? "" : ("?" + ServerConstant.SiteInfo.VNSHARING_QUERRY_ALPHABETICAL + ("#".equalsIgnoreCase(childSiteId) ? "0" : childSiteId));
			Connection conn = ConnectionMgr.INSTANCE.connect(url);
			Document listPage = conn.get();
			Element elementTagA = listPage.select("#leftside > div > div:eq(2) > ul > li:last-child > a").first();
			int mangaPerPage = listPage.select("#leftside > div > div:eq(1) > div:eq(3) > table > tbody > tr").size() - 2;
			int mangaCount = mangaPerPage;
			int lastListPageIdx = 1;
			try {
				lastListPageIdx = Integer.parseInt(elementTagA.attr(ServerConstant.HtmlTags.ATT_A_PAGE));
			} catch (NullPointerException e) {

			}
			if (lastListPageIdx > 1) {
				url += (url.contains("?") ? "&" : "?") + ServerConstant.SiteInfo.VNSHARING_QUERRY_PAGE + lastListPageIdx;
				conn = ConnectionMgr.INSTANCE.connect(url);
				listPage = conn.get();
				int mangaInLastListPage = listPage.select("#leftside > div > div:eq(1) > div:eq(3) > table > tbody > tr").size() - 2;
				mangaCount += mangaPerPage * (lastListPageIdx - 2) + mangaInLastListPage;
			}
			result.setPageCount(lastListPageIdx);
			result.setMangaCount(mangaCount);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public SitePageInfo getSitePageInfo(String childSiteId, String pageIndex) {
		SitePageInfo result = new SitePageInfo();

		try {
			String url = ServerConstant.Protocols.HTTP + ServerConstant.SiteInfo.VNSHARING_BASE_URL + ServerConstant.SiteInfo.VNSHARING_LIST_PATH;
			url += "?" + (childSiteId.length() == 0 ? "" : (ServerConstant.SiteInfo.VNSHARING_QUERRY_ALPHABETICAL + ("#".equalsIgnoreCase(childSiteId) ? "0" : childSiteId)));
			url += (url.contains("?") ? "&" : "?") + ServerConstant.SiteInfo.VNSHARING_QUERRY_PAGE + pageIndex;
			Connection conn = ConnectionMgr.INSTANCE.connect(url);
			Document listPage = conn.get();
			List<MangaInfo> lstMangaInfo = parse(listPage);
			result.setIndex(Integer.parseInt(pageIndex));
			result.setCount(lstMangaInfo.size());
			result.setLstMangaInfo(lstMangaInfo);
		} catch (IOException e) {
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public MangaDesc getMangaDesciption(String mangaLink) {
		MangaDesc result = new MangaDesc();
		
		try {
			Connection conn = ConnectionMgr.INSTANCE.connect(mangaLink);
			Document page = conn.get();
			Elements table = page.select("#leftside > div:eq(2) > div:eq(1) > div:eq(1) > table");
			result.setContent(table.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return result;
	}

	private List<MangaInfo> parse(Document doc) {
		List<MangaInfo> result = new ArrayList<MangaInfo>();
		Elements rows = doc.select("#leftside > div > div:eq(1) > div:eq(3) > table > tbody > tr");
		for (Element row : rows) {
			try {
				MangaInfo mangaInfo = new MangaInfo();
				Element columnManga = row.select("td:eq(0) > a").first();
				if (columnManga.text() != null) {
					String hrefAttribute = columnManga.attr(ServerConstant.HtmlTags.ATT_A_HREF);
					String mangaID = hrefAttribute.substring(hrefAttribute.lastIndexOf("?id=") + 4);
					String uniqueName = hrefAttribute.substring(hrefAttribute.lastIndexOf("/") + 1, hrefAttribute.lastIndexOf("?id="));
					mangaInfo.setId(mangaID);
					mangaInfo.setDisplayName(columnManga.text().trim());
					mangaInfo.setUniqueName(uniqueName);
					mangaInfo.setPreview(columnManga.attr(ServerConstant.HtmlTags.ATT_GLOBAL_TITLE));
					mangaInfo.setLink(ServerConstant.Protocols.HTTP + ServerConstant.SiteInfo.VNSHARING_BASE_URL + columnManga.attr(ServerConstant.HtmlTags.ATT_A_HREF));
				}
				Element columnGroup = row.select("td:eq(1) > a").first();
				if (columnGroup.text() != null) {
					mangaInfo.getGroup().setName(columnGroup.text());
					mangaInfo.getGroup().setLink(ServerConstant.Protocols.HTTP + ServerConstant.SiteInfo.VNSHARING_BASE_URL + columnGroup.attr(ServerConstant.HtmlTags.ATT_A_HREF));
				}
				Element columnLatestChapter = row.select("td:eq(2)").first();
				Element elementTagA = columnLatestChapter.select("a").first();
				if (elementTagA != null) {
					mangaInfo.setLatestChapter(new ChapterInfo(elementTagA.text(), ServerConstant.Protocols.HTTP + ServerConstant.SiteInfo.VNSHARING_BASE_URL + elementTagA.attr(ServerConstant.HtmlTags.ATT_A_HREF)));
				} else {
					mangaInfo.setLatestChapter(new ChapterInfo(columnLatestChapter.text()));
				}

				result.add(mangaInfo);

			} catch (NullPointerException e) {
				continue;
			}
		}
		return result;
	}
}
